package com.heyi.saas.start.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.heyi.saas.start.cache.TenantIdKeyGenerator;
import com.heyi.saas.start.constant.SaasConstant;
import com.heyi.saas.start.service.ShareDataService;
import com.heyi.saas.start.service.TenantContext;
import com.heyi.saas.start.service.TenantTables;
import com.heyi.saas.start.service.impl.ShareDataServiceImpl;
import com.heyi.saas.start.util.oConvertUtils;
import lombok.SneakyThrows;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.Update;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * 为mybatis plus设置租户的TenantLineInnerInterceptor
 * @author dengfeng
 * @date 2021/9/1716:35
 */
@Configuration
public class MybatisPlusTenantConfig {

    @Autowired
    public TenantTables tenantTables;

    @Bean
    public ShareDataServiceImpl getShareDataServiceImpl(){
        return new ShareDataServiceImpl();
    }

    /** 功能描述
     　* @Description: 重写TenantLineInnerInterceptor，加入数据权限的条件
     　* @author dengfeng
     　* @date 2021/8/9 18:42
     　*/
    @Bean
    public TenantLineInnerInterceptor getTenantLineInnerInterceptor() {
        TenantLineHandler tenantLineHandler = new MyTenantLineHandler(tenantTables);
        return new MyTenantLineInnerInterceptor(tenantLineHandler);
    }

    /** 功能描述
     　* @Description: 注入自定义cache key生成器
     　* @author dengfeng
     　* @date 2021/8/12 11:51
     　*/
    @Bean
    public TenantIdKeyGenerator tenantIdKeyGenerator(){
        return new TenantIdKeyGenerator();
    }

    /** 功能描述
    　* @Description: 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
    　* @author dengfeng
    　* @date 2021/9/17 17:37
    　*/
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}
